# -*- coding: utf-8 -*-
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *

import math

mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Part-1', type=DEFORMABLE_BODY)

# 胞元几何形状参数
H = 10.0
D1 = 3.0 # 内圆直径
D2 = 6.0 # 外圆直径
R1 = 0.5 * D1 # 内圆半径
R2 = 0.5 * D2 # 外圆半径

# 阵列数目
X_N = 5
Y_N = 3

# 阵列间距
X_SPACE = H
Y_SPACE = 3 ** 0.5 * H

def sin(theta):
  return math.sin(theta / 180.0 * math.pi)

def cos(theta):
  return math.cos(theta / 180.0 * math.pi)


# 生成胞元
def createStar():

  # 六角星的12个顶点(最右侧为编号1，逆时针编号)
  point_1 = (R1 * cos(0), R1 * sin(0), 0)
  point_2 = (R2 * cos(30), R2 * sin(30), 0)
  point_3 = (R1 * cos(60), R1 * sin(60), 0)
  point_4 = (R2 * cos(90), R2 * sin(90), 0)
  point_5 = (R1 * cos(120), R1 * sin(120), 0)
  point_6 = (R2 * cos(150), R2 * sin(150), 0)
  point_7 = (R1 * cos(180), R1 * sin(180), 0)
  point_8 = (R2 * cos(210), R2 * sin(210), 0)
  point_9 = (R1 * cos(240), R1 * sin(240), 0)
  point_10 = (R2 * cos(270), R2 * sin(270), 0)
  point_11 = (R1 * cos(300), R1 * sin(300), 0)
  point_12 = (R2 * cos(330), R2 * sin(330), 0)

  # 组成六角星的12条线段
  line_1 = (point_1, point_2)
  line_2 = (point_2, point_3)
  line_3 = (point_3, point_4)
  line_4 = (point_4, point_5)
  line_5 = (point_5, point_6)
  line_6 = (point_6, point_7)
  line_7 = (point_7, point_8)
  line_8 = (point_8, point_9)
  line_9 = (point_9, point_10)
  line_10 = (point_10, point_11)
  line_11 = (point_11, point_12)
  line_12 = (point_12, point_1)

  # 六角星外部的6根连杆顶点
  link_point_1 = (0.5 * H * cos(0), 0.5 * H * sin(0), 0)
  link_point_2 = (0.5 * H * cos(60), 0.5 * H * sin(60), 0)
  link_point_3 = (0.5 * H * cos(120), 0.5 * H * sin(120), 0)
  link_point_4 = (0.5 * H * cos(180), 0.5 * H * sin(180), 0)
  link_point_5 = (0.5 * H * cos(240), 0.5 * H * sin(240), 0)
  link_point_6 = (0.5 * H * cos(300), 0.5 * H * sin(300), 0)

  # 组成六星形胞元外部的6根连杆
  link_1 = (link_point_1, point_1)
  link_2 = (link_point_2, point_3)
  link_3 = (link_point_3, point_5)
  link_4 = (link_point_4, point_7)
  link_5 = (link_point_5, point_9)
  link_6 = (link_point_6, point_11)

  lines = [line_1, line_2, line_3, line_4,
           line_5, line_6, line_7, line_8,
           line_9, line_10, line_11, line_12,
           link_1, link_2, link_3, link_4, link_5, link_6]

  return lines

# 沿x轴、y轴阵列线段
def arrayStar(x_n, y_n, starLines):

  X_arrayLines = []
  Y_arrayLines = []

  # 胞元沿x轴进行阵列
  for i in range(x_n):

    for item in starLines:

      point_x_1 = (item[0][0] + X_SPACE * i, item[0][1], item[0][2])
      point_x_2 = (item[1][0] + X_SPACE * i, item[1][1], item[1][2])
      
      X_arrayLines.append((point_x_1, point_x_2))
  
  # 胞元沿y轴进行阵列
  for j in range(y_n):

    for each in X_arrayLines:

      point_y_1 = (each[0][0], each[0][1] + Y_SPACE * j, each[0][2])
      point_y_2 = (each[1][0], each[1][1] + Y_SPACE * j, each[1][2])

      Y_arrayLines.append((point_y_1, point_y_2))

  return Y_arrayLines

# 移动线段
def move(offset_x, offset_y, lines):

  res = []

  for line in lines:
    point_1 = (line[0][0] + offset_x, line[0][1] + offset_y, 0)
    point_2 = (line[1][0] + offset_x, line[1][1] + offset_y, 0)
    res.append((point_1, point_2))

  return res

# 填补中间空缺的六角星
def makeUpStar():

  # 生成六角星
  starLines = createStar()

  offset_x = 0.5 * H
  offset_y = 0.5 * 3 ** 0.5 * H
  
  # 偏移六角星
  lines = move(offset_x, offset_y, starLines)
  
  # 阵列六角星
  lines += arrayStar(X_N - 1, Y_N - 1, lines)

  return lines

lines = []

starLines = createStar()

lines = arrayStar(X_N, Y_N, starLines)

lines += makeUpStar()

lines_tuple = tuple(lines)

print(" ")

# 打印参数
print("H: %s " % H)
print("D1: %s " % D1)
print("D2: %s " % D2)
print("X_N: %d " % X_N)
print("Y_N: %d " % Y_N)

mdb.models['Model-1'].parts['Part-1'].WirePolyLine(mergeType=IMPRINT, meshable=ON, 
    points=lines_tuple)

mdb.models['Model-1'].parts['Part-1'].Set(edges=
    mdb.models['Model-1'].parts['Part-1'].edges.getSequenceFromMask(('[#3 ]', 
    ), ), name='Wire-2-Set-1')


# Save by cgp on 2021_06_01-17.37.10; build 6.14-5 2015_08_18-22.37.49 135153
